package com.ken.user.controller;

import com.ken.common.pojo.*;
import com.ken.common.pojo.Class;
import com.ken.common.result.Result;
import com.ken.common.utils.QRUtil;
import com.ken.common.vo.captchaVo;

import com.ken.fx.TextMain;
import com.ken.fx.UniversalCharacterRecognition;
import com.ken.fx.WebITS;
import com.ken.fx.WebTextCorrection;
import com.ken.user.dto.*;
import com.ken.user.service.userService;
import com.ken.user.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
//@CrossOrigin("*")
@RequestMapping("/user")
public class userController {
    @Autowired
    private userService userService;

    /**
     * 获取登录的图片验证码
     * @return captchaVo中存有验证码的key和图片编码
     */
    @GetMapping("/getCaptcha")
    public Result<captchaVo> getCaptcha(){
        captchaVo captcha = userService.getCaptcha();
        return Result.success(captcha);
    }

    /**
     * 发送验证码
     * @param sendMessageDto 包含发送验证码的模板类型，手机号
     * @return 返回发送信息
     * @throws Exception
     */
    @PostMapping("/sendMessage")
    public Result<String> sendMessage(@RequestBody sendMessageDto sendMessageDto) throws Exception {
        return userService.sendMessage(sendMessageDto);
    }

//    /**
//     * 用户密码登录接口
//     * @param loginWithPasswordDto 里面包含了用户输入的手机号，密码，验证码,验证码的key
//     * @return
//     */
//    @PostMapping("/loginWithPassword")
//    public Result loginWithPassword(@RequestBody loginWithPasswordDto loginWithPasswordDto){
//        return userService.loginWithPassword(loginWithPasswordDto);
//    }

    /**
     * 使用人脸登录
     * @param loginWithFaceDto 里面包含要登陆的手机号，照片的base64字节编码，图片的后缀
     * @return 返回的data中包含jwt令牌
     * @throws Exception
     */
    @PostMapping("/loginWithFace")
    public Result loginWithFace(@RequestBody loginWithFaceDto loginWithFaceDto) throws Exception {
        return  userService.loginWithFace(loginWithFaceDto);
    }

    /**
     * 图片提取文字
     * @param img 图片的base64字节编码
     * @return data中附带提取的文字
     * @throws Exception
     */
    @PostMapping("/imgToText")
    public Result<String> imgToText(@RequestBody imgToTextDto img) throws Exception {
        //todo 后面换成openfeign调用
        String text = UniversalCharacterRecognition.getText(img.getImgBase64());
        return Result.success(text);
    }

    /**
     * 短信登录
     * @param loginWithPhone 包括登录的手机号与验证码
     * @return 如果登录成功返回jwt令牌否则返回错误信息
     */
    @PostMapping("loginWithPhone")
    public Result loginWithPhone(@RequestBody loginWithPhone loginWithPhone){
        return userService.loginWithPhone(loginWithPhone);
    }

    /**
     * 进行手机号验证
     * @param registerCheckDto 包含手机号与验证码
     * @return 返回验证信息
     */
    @PostMapping("/registerCheck")
    public Result registerCheck(@RequestBody registerCheckDto registerCheckDto){
        return userService.registerCheck(registerCheckDto);
    }

    /**
     * 在手机号验证后进行老师注册
     * @param teacher 包含需要填写的信息
     * @return 返回注册信息
     */
    @PostMapping("/teacherRegister")
    public Result<String> teacherRegister(@RequestBody teacher teacher){
        return  userService.teacherRegister(teacher);
    }

    /**
     * 在手机验证后进行学生注册
     * @param student 包含需要填写的信息
     * @return 返回注册信息
     */
    @PostMapping("/studentRegister")
    public Result<String> studentRegister(@RequestBody student student){
        log.info("学生注册"+student);
        return  userService.studentRegister(student);
    }

    /**
     * 使用学工号密码登录
     * @param login 传入登录所需信息
     * @return 登录成功返回jwt令牌否则返回错误信息
     */
    @PostMapping("/loginWithPassword")
    public Result loginWithPassword(@RequestBody loginWithPasswordDto login){
        return userService.loginWithPassword(login);
    }

    /**
     * 用户加入班级的接口
     * @param dto 内涵加入班级所需属性
     * @return 返回加入信息
     */
    @PostMapping("/joinClassDto")
    public Result<String> joinClass(@RequestBody joinClassDto dto){
        log.info(dto.toString());
        return userService.joinClass(dto);
    }

    /**
     * 获取指定班级的全部学生
     * @param class_id 指定班级的班级Id
     * @return 返回学生的集合
     */
    @GetMapping("/getClassStudent/{class_id}")
    public Result getClassStudent(@PathVariable String class_id){
        return userService.getClassStudent(class_id);
    }

    /**
     * 利用作业id获取作业中绑定问题的情况
     * @param homework_id 作业id
     * @return
     */
    @GetMapping("/getQuestionByHomework/{homework_id}")
    public Result<List<questionVo>> getQuestionByHomework(@PathVariable String homework_id){
        List<questionVo> questionByHomework = userService.getQuestionByHomework(homework_id);
        return Result.success(questionByHomework);
    }

    /**
     * 根据班级id获取发布的作业情况
     * @param class_id 班级id
     * @return
     */
    @GetMapping("/getHomeworkByClass/{class_id}")
    public Result<List<homeworkVo>> getHomeworkByClass(@PathVariable String class_id){
        List<homeworkVo> homeworkByClass = userService.getHomeworkByClass(class_id);
        return Result.success(homeworkByClass);
    }

    /**
     * 根据班级id获取班级所有的考试信息
     * @param class_id 班级id
     * @return 返回带有所有考试信息的数组
     */
    @GetMapping("/getExamByClass/{class_id}")
    public Result<List<examVo>> getExamByClass(@PathVariable String class_id){
        List<examVo> exams = userService.getExamByClass(class_id);
        return Result.success(exams);
    }

    /**
     * 根据试卷id获取试卷的信息
     * @param paper_id 试卷的id
     * @return 返回查询到的试卷信息
     */
    @GetMapping("/getPaperById/{paper_id}")
    public Result<paperVo> getPaperById(@PathVariable String paper_id){
        paperVo paper = userService.getPaperById(paper_id);
        return Result.success(paper);
    }

    /**
     * 使用试卷id获取试卷绑定的题目情况
     * @param paper_id 试卷id
     * @return 返回装有问题的数组
     */
    @GetMapping("/getQuestionByPaper/{paper_id}")
    public Result<List<questionVo>> getQuestionByPaper(@PathVariable String paper_id){
        List<questionVo> questions = userService.getQuestionByPaper(paper_id);
        return Result.success(questions);
    }

    /**
     * 根据学号和作业号获取学生回答
     * @param student_id 学生id
     * @param homework_id 作业id
     * @return 返回一个数组，包括作业每个题目的回答和题目id
     */
    @GetMapping("/get_answerByHomework")
    public Result<List<answerVo>> get_answerByHomework(@RequestParam String student_id,@RequestParam String homework_id){
        List<answerVo> answerByHomework = userService.get_answerByHomework(student_id, homework_id);
        return Result.success(answerByHomework);
    }

    /**
     * 通过学号和作业号获取学生的作业批改情况
     * @param student_id 学生的id
     * @param homework_id 作业的id
     * @return 返回作业的情况
     */
    @GetMapping("/get_homeworkState")
    public Result<homework_stateVo> get_homeworkState(@RequestParam String student_id,@RequestParam String homework_id){
        homework_stateVo homeworkState = userService.get_homeworkState(student_id, homework_id);
        return Result.success(homeworkState);
    }

    /**
     * 保存错题
     * @param dto 包含所需要的字段信息
     * @return
     */
    @PostMapping("save_wrongQuestion")
    public Result<String> save_wrongQuestion(wrongQuestionDto dto){
        userService.save_wrongQuestion(dto);
        return Result.success("保存错题成功");
    }

    /**
     * 通过课程号获取单元情况
     * @param course_id 课程号
     * @return 存有单元信息的数组
     */
    @GetMapping("/getUnitByCourse")
    public Result<List<unitVo>> getUnitByClass(@RequestParam String course_id){
        List<unitVo> unitByClass = userService.getUnitByClass(course_id);
        return Result.success(unitByClass);
    }

    /**
     * 通过单元id获取课时情况
     * @param unit_id 单元id
     * @return 存有课时信息的数组
     */
    @GetMapping("/getLessonByUnit")
    public Result<List<lessonVo>> getLessonByUnit(String unit_id){
        List<lessonVo> lessonByUnit = userService.getLessonByUnit(unit_id);
        return Result.success(lessonByUnit);
    }

    /**
     * 上传学生学习情况的接口
     * @param dto 所需的数据
     * @return 返回总时间
     */
    @PostMapping("/upload_studyRecord")
    public Result<Long> upload_studyRecord(@RequestBody studyRecordDto dto){
        long time = userService.upload_studyRecord(dto);
        return Result.success(time);
    }

    /**
     * 根据学生id获取学习情况
     * @param student_id 学生id
     * @return 返回学习情况
     */
    @GetMapping("/getRecordById")
    public Result<List<studyRecord>> getRecordById(@RequestParam("student_id") String student_id){
        List<studyRecord> records = userService.getRecordById(student_id);
        //todo 返回更加详细的信息
        return Result.success(records);
    }

    /**
     * 获取课时学习时间
     * @param student_id 学生id
     * @param lesson_id 课时id
     * @return 总时间单位为秒
     */
    @GetMapping("/getLessonTotalTime")
    public Result<Long> getLessonTotalTime(@RequestParam("student_id") String student_id,@RequestParam("lesson_id") String lesson_id){
        Long totalTime = userService.getLessonTotalTime(student_id, lesson_id);
        return Result.success(totalTime);
    }
    /**
     * 根据班级id获取班级信息
     * @param class_id 班级id
     * @return 指定的班级信息
     */
    @GetMapping("/getClassByClassId")
    public Result<Class> getClassByClassId(@Param("class_id") String class_id){
        Class aClass = userService.getClassByClassId(class_id);
        return Result.success(aClass);
    }
    /**
     * 根据课程号获取所有班级信息
     * @param course_id 课程id
     * @return 课程信息数组
     */
    @GetMapping("/getClassByCourseId")
    public Result<List<Class>> getClassByCourseId(@Param("course_id") String course_id){
        System.out.println(course_id);
        List<Class> classes = userService.getClassByCourseId(course_id);
        return Result.success(classes);
    }
    /**
     * 上传word进行翻译
     * @param multipartFile 上传的word
     * @param from 原语言
     * @param to 目标语言
     * @return 返回翻译后的word的url
     * @throws Exception
     */
    @PostMapping("/translation_word")
    public Result<String> translation_word(@RequestParam("file") MultipartFile multipartFile,@RequestParam("from") String from,@RequestParam("to") String to) throws Exception {
        String url = userService.translation_word(multipartFile,from,to);
        return Result.success(url);
    }

    /**
     * 进行翻译
     * @param dto 进行翻译所需的数据
     * @return 返回翻译后的文本
     * @throws Exception
     */
    @PostMapping("/translation")
    public Result<String> translation(@RequestBody translationDto dto) throws Exception {
        return Result.success(WebITS.translation(dto.getText(),dto.getFrom(),dto.getTo()));
    }

    /**
     * 语音口语评测
     * @param file 音频文件pcm
     * @param language 设置评测语言中文：cn_vip 英文：en_vip
     * @param change_language 设置音频转换语言中文：zh_cn,英文：en_us,法语：fr_fr,日语：日语
     * @return 返回评分与文本
     * @throws Exception
     */
    @PostMapping("/commentary")
    public Result<commentaryVo> commentary(@RequestParam("file") MultipartFile file,@RequestParam("language") String language,@RequestParam("change_language") String change_language) throws Exception {
        commentaryVo commentary = userService.commentary(file,language,change_language);
        return Result.success(commentary);
    }

    /**
     * 将文本转成语音
     * @param text 文本内容
     *
     * @param language 要转成的语言：中文，英文，法语，德语
     * @return 返回音频的url
     * @throws Exception
     */
    @PostMapping("/SpeechToText")
    public Result<String> SpeechToText(@RequestParam("text") String text ,@RequestParam("language") String language) throws Exception {
        String url = userService.SpeechToText(text, language);
        return Result.success(url);
    }

    /**
     * 用于检查作业是否过期，若过期将会自动更改所有状态，并且会返回作业的剩余时间
     * @param dto 作业id与班级id
     * @return
     */
    @PostMapping("/check_homework")
    public Result<String> check_homework(@RequestBody check_homeworkDto dto){
        return userService.check_homework(dto);
    }

    /**
     * 通过课程id获取作业
     * @param course_id 课程id
     * @return 返回作业的数组
     */
    @GetMapping("/getHomeworkByCourseId")
    public Result<List<homework>> getHomeworkByCourseId(@Param("course_id") String course_id){
        List<homework> homeworks = userService.getHomeworkByCourseId(course_id);
        return Result.success(homeworks);
    }

    /**
     * 根据问题id获取问题信息
     * @param question_id 要获取的问题id
     * @return 返回问题id详细信息
     */
    @GetMapping("/getQuestionById")
    public Result<Question> getQuestionById(@Param("question_id") String question_id){
        Question question = userService.getQuestionById(question_id);
        return Result.success(question);
    }

    /**
     * 根据班级id获取发布的消息
     * @param class_id 班级id
     * @return 返回消息数组
     */
    @GetMapping("/getMessageByClassId")
    public Result<getMessageDto> getMessageByClassId(@Param("class_id") String class_id){
        getMessageDto message = userService.getMessageByClassId(class_id);
        return Result.success(message);
    }

    /**
     * 根据作业id获取作业内容
     * @param homework_id 作业id
     * @return 返回作业内容
     */
    @GetMapping("getHomeworkContent")
    public Result<homework> getHomeworkContent(@Param("homework_id") String homework_id){
        homework homeworkContent = userService.getHomeworkContent(homework_id);
        return Result.success(homeworkContent);
    }

    /**
     * 获取签到情况
     * @param code 签到码
     * @return 返回签到情况
     */
    @GetMapping("/getAttendanceRecordState")
    public Result<List<attendance>> getAttendanceRecordState(@Param("code") String code){
        List<attendance> attendanceRecordList = userService.getAttendanceRecordState(code);
        return Result.success(attendanceRecordList);
    }

    /**
     * 获取签到码二维码
     * @param code 签到码
     * @return 签到二维码的url
     * @throws Exception
     */
    @GetMapping("/getAttendanceImg")
    public Result<String> getAttendanceImg(@RequestParam("code") String code) throws Exception {
        return Result.success( QRUtil.createQR(code));
    }
    /**
     * 更改message的状态
     * @param student_id 学生id
     * @param message_id 信息的id
     * @return 返回结果
     */
    @PostMapping("/change_messageState")
    public  Result<String> change_messageState(@Param("student_id") String student_id,@Param("message_id") String message_id){
        userService.change_messageState(student_id,message_id);
        return Result.success("更改成功");
    }

    /**
     * 上传文件资源接口
     * @param file 文件
     * @param extension 文件后缀名
     * @return 返回文件资源的url
     * @throws Exception
     */
    @PostMapping("/upload_file")
    public Result<String> upload_file(@RequestParam("file") MultipartFile file,@RequestParam("extension") String extension) throws Exception {
        String url = userService.upload_file(file, extension);
        return Result.success(url);
    }

    /**
     * 保存知识点用于构建知识图谱
     * @param knowledgePoint 知识点
     * @return 返回保存情况
     */
    @PostMapping("/create_knowledgePoints")
    public Result<String> create_knowledgePoints(@RequestBody knowledgePointsDto knowledgePoint){
        for(knowledgePoint knowledgePoint1 : knowledgePoint.getKnowledgePoint()){
            userService.create_knowledgePoints(knowledgePoint1);
        }
        return Result.success("保存知识点成功");
    }

    /**
     * 获取知识图谱
     * @param root 根节点的id
     * @return 返回根节点的所有知识点
     */
    @GetMapping("/getKnowledgePoints")
    public Result<List<knowledgePoint>> getKnowledgePoints(@RequestParam("root") String root){
        List<knowledgePoint> knowledgePoints = userService.getKnowledgePoint(root);
        return Result.success(knowledgePoints);
    }

    /**
     * 删除知识点
     * @param roots 知识点的根节点
     * @return 返回删除情况
     */
    @DeleteMapping("/delete_knowledgePoints")
    public Result<String> delete_knowledgePoints(@RequestParam("roots") List<String> roots){
        userService.delete_knowledgePoints(roots);
        return Result.success("删除成功");
    }

    /**
     * 修改知识点
     * @param knowledgePoint 知识点信息
     * @return 返回修改结果
     */
    @PostMapping("/update_knowledgePoint")
    public Result<String> update_knowledgePoint(@RequestBody knowledgePoint knowledgePoint){
        userService.update_knowledgePoint(knowledgePoint);
        return Result.success("修改成功");
    }
    /**
     * 批量保存题目
     * @param questionList 题目列表
     * @return 返回保存的题目的所有id
     */
    @PostMapping("/save_questions")
    public Result<List<String>> save_questions(@RequestBody save_questionsDto questionList){
        log.info("保存问题"+questionList.toString());
        List<String> strings = userService.save_questions(questionList.getQuestionList());
        return Result.success(strings);
    }

    /**
     * 根据课时id获取课时详情
     * @param lessons_Id 课时id
     * @return 返回课时详情
     */
    @GetMapping("/getLessonById")
    public Result<lessons> getLessonById(@RequestParam("lessons_id") String lessons_Id){
        lessons lesson = userService.getLessonById(lessons_Id);
        return Result.success(lesson);
    }

    /**
     * 根据单元id获取所有的课时
     * @param unit_id 单元id
     * @return 返回所有课时的信息
     */
    @GetMapping("/getLessonsByUnit")
    public Result<List<lessons>> getLessonsByUnit(@RequestParam("unit_id") String unit_id){
        List<lessons> lessons = userService.getLessonsByUnit(unit_id);
        return Result.success(lessons);
    }

    /**
     * 更新知识图谱
     * @param knowledgePoints 更新后的知识图谱情况
     * @return 返回更新情况
     */
    @PostMapping("/update_knowledgePoints")
    public Result<String> update_knowledgePoints(@RequestBody List<knowledgePoint> knowledgePoints){
        userService.update_knowledgePoints(knowledgePoints);
        return Result.success("更新知识图谱成功");
    }

    /**
     * 根据id获取知识点
     * @param knowledgePointId 知识点id
     * @return 返回信息
     */
    @GetMapping("/getKnowledgePointById")
    public Result<knowledgePoint> getKnowledgePointById(@RequestParam("knowledgePointId") String knowledgePointId){
        knowledgePoint point = userService.getKnowledgePointById(knowledgePointId);
        return Result.success(point);
    }

    /**
     * 删除课程的知识点
     * @param lesson_id 课程id
     * @return 返回删除信息
     */
    @DeleteMapping("/delete_lessonKnowledge")
    public Result<String> delete_lessonKnowledge(@RequestParam("lesson_id") String lesson_id){
        userService.delete_lessonKnowledge(lesson_id);
        return Result.success("删除成功");
    }

    /**
     * 检查签到消息是否过期
     * @param message_id 消息id
     * @return 返回信息
     */
    @GetMapping("/check_message")
    public Result<String> check_message(@RequestParam("message_id") String message_id){
        return userService.check_message(message_id);
    }

    /**
     * 文本纠错信息
     * @param text 文本
     * @return 返回纠错信息
     * @throws Exception
     */
    @GetMapping("/text_correct")
    public Result<String> text_correct(@RequestParam("text") String text) throws Exception {
        return Result.success(WebTextCorrection.textCorrection(text));
    }

    /**
     * 文本合规
     * @param text 文本
     * @return 返回合规信息
     * @throws Exception
     */
    @GetMapping("/text_compliance")
    public Result<String> text_compliance(@RequestParam("text") String text) throws Exception {
        return Result.success(TextMain.text_compliance(text));
    }

    /**
     * 用于更新用户信息
     * @param dto 用户信息
     * @return 返回更改结果
     */
    @PostMapping("/update_user")
    public Result<String> update_user(@RequestBody update_userDto dto){
        userService.update_user(dto);
        return Result.success("更新成功");
    }

    /**
     * 修改用户头像
     * @param dto 修改信息
     * @return 返回修改结果
     */
    @PostMapping("/update_img")
    public Result<String> update_img(@RequestBody update_imgDto dto) throws Exception {
        userService.update_img(dto);
        return Result.success("修改成功");
    }

    /**
     * 获取用户信息
     * @param phone 手机号
     * @return 返回用户信息
     */
    @GetMapping("/getUserInfo")
    public Result<user> getUserInfo(@RequestParam("phone") String phone){
        user userInfo = userService.getUserInfo(phone);
        return Result.success(userInfo);
    }

    /**
     * 获取所有课程
     * @return 返回课程信息
     */
    @GetMapping("/getAllCourse")
    public Result<List<course>> getAllCourse(){
        List<course> allCourse = userService.getAllCourse();
        return Result.success(allCourse);
    }

    /**
     * 上传图片资源
     * @param dto 图片base64编码
     * @return 返回资源url
     * @throws Exception
     */
    @PostMapping ("/post_img")
    public Result<String> post_img(@RequestBody post_imgDto dto) throws Exception {
        String url = userService.post_img(dto.getImgBase64());
        return Result.success(url);
    }
}
